package com.sino.operation.util;

import com.adtec.ms.msagent.service.AddressingService;
import com.adtec.ms.msagent.service.AgentMngService;
import com.adtec.ms.msagent.service.QualityService;
import com.adtec.ms.msagent.service.impl.AgentMngServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName EscUtil
 * @Description esc api封装
 * @Author yangfangyu
 * @Date 2021/9/14 17:03
 * @Version 1.0
 **/
@PropertySource("classpath:param.properties")
@Log4j2
@Component
public class EscUtil {

    @Resource
    Environment env;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @Author fangyu_yang
     * @Description 获取AddressingService
     * @Date 17:30 2021/9/14
     * @Param []
     * @return com.adtec.ms.msagent.service.AddressingService
     **/
    public AddressingService getApplicationContext(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        AddressingService addressingService= (AddressingService) ctx.getBean("addressingService");
        return addressingService;
    }

    public  Map<String,Object> escTest(String escToken,String tenant,String partId,String svrName){
        String seqNo = UUID.randomUUID().toString();
        AddressingService applicationContext = getApplicationContext();
        HashMap<String, Object> svrAddr=applicationContext.getSvrAddr(seqNo, escToken, tenant, partId, svrName,null);
        return svrAddr;
    }

    /**
     * @Author fangyu_yang
     * @Description 寻址
     * @Date 18:12 2021/9/14
     * @Param [svrName]
     * @return boolean
     **/
    public Map getSvrAddr(String svrName){
        log.info("寻址开始-getSvrAddr,参数："+svrName);
        AddressingService applicationContext = getApplicationContext();
        //token存放在redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String escToken = ops.get("EscToken");
        String seqNo = UUID.randomUUID().toString();
        String tenant =env.getProperty("ChannelTenant");
        String partId =env.getProperty("ChannelPartId");
        HashMap<String, Object> svrAddr = applicationContext.getSvrAddr(seqNo, escToken, tenant, partId, svrName,null);
        log.info("寻址结束-getSvrAddr");
        return svrAddr;
    }

    /**
     * @Author fangyu_yang
     * @Description 服务收集 消费之前
     * @Date 15:11 2021/9/18
     * @Param [seqNo, url, sqNode, svrName, version, timeOut, buff]
     * @return java.util.HashMap<java.lang.String,java.lang.Object>
     **/
    public HashMap<String, Object> requestStart(String seqNo,String url,String sqNode,String svrName,String version,int timeOut,String buff){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        QualityService qualityService = (QualityService) ctx.getBean("qualityService");
        //token存放在redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String escToken = ops.get("EscToken");

        //消费调用服务开始
        //tenant：服务方租户(必输)
        String tenant = env.getProperty("ChannelTenant");
        //	partId：服务方参与者ID(必输)
        String partId = env.getProperty("ChannelPartId");
        //	partVersion：服务方参与者版本(必输)
        String partVersion = env.getProperty("ChannelPartVersion");
        //partInst：服务方参与者实例(必输)
        String partInst = env.getProperty("ChannelPartInst");

        HashMap<String, Object> start = qualityService.requestStart(escToken, seqNo, "", "V1.0", tenant, partId, partVersion, partInst, svrName
                , version, timeOut, buff.getBytes());
        return start;
    }

    /**
     * @Author fangyu_yang
     * @Description 服务收集 消费之后
     * @Date 15:15 2021/9/18
     * @Param [seqNo, url, sqNode, svrName, version, timeOut, retConnCode, retAppCode, msg, buff]
     * @return java.util.HashMap<java.lang.String,java.lang.Object>
     **/
    public HashMap<String, Object> requestEnd(String seqNo,String url,String sqNode,String svrName,String version,int timeOut,String retConnCode, String retAppCode, String msg,String buff){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        QualityService qualityService = (QualityService) ctx.getBean("qualityService");
        //token存放在redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String escToken = ops.get("EscToken");

        //消费调用服务开始
        //tenant：服务方租户(必输)
        String tenant = env.getProperty("ChannelTenant");
        //	partId：服务方参与者ID(必输)
        String partId = env.getProperty("ChannelPartId");
        //	partVersion：服务方参与者版本(必输)
        String partVersion = env.getProperty("ChannelPartVersion");
        //partInst：服务方参与者实例(必输)
        String partInst = env.getProperty("ChannelPartInst");

        HashMap<String, Object> end = qualityService.requestEnd(escToken, seqNo, tenant, partId, partVersion, partInst, svrName
                , version, retConnCode,retAppCode, msg,buff.getBytes());
        return end;
    }


    /**
     * @Author fangyu_yang
     * @Description 开启esc服务
     * @Date 9:42 2021/9/15
     * @Param []
     * @return java.lang.String
     **/
    public String startAgent(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        AgentMngService agentMngService = (AgentMngService) ctx.getBean("agentMngService");
        AgentMngServiceImpl serviceImpl = new AgentMngServiceImpl();
        ArrayList<HashMap<String, Object>> loginInfos = Lists.newArrayList();
        HashMap<String, Object> loginInfo = Maps.newHashMap();
        loginInfo.put("Tenant",env.getProperty("Tenant"));
        loginInfo.put("PartId",env.getProperty("PartId"));
        loginInfo.put("PartVersion",env.getProperty("PartVersion"));
        loginInfo.put("LoginUser",env.getProperty("LoginUser"));
        loginInfo.put("LoginPassword",env.getProperty("LoginPassword"));
        loginInfo.put("HostName",env.getProperty("HostName"));
        loginInfo.put("Port",env.getProperty("Port"));
        loginInfos.add(loginInfo);
        HashMap<String, Object> map = agentMngService.startAgent(loginInfos);
        List<Map<String,Object>> results = (List<Map<String,Object>>)map.get("Results");
        String token =null;
        for (Map<String, Object> m : results) {
             token = (String)m.get("Token");
        }
        //token存入redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("EscToken",token);
        return token;
    }






}
